package com.fyzk.activity.lottery.recommend_order;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ProgressBar;

import com.fyzk.base.BaseActivity;
import com.tyjc.lottery.BuildConfig;
import com.tyjc.lottery.R;
import com.fyzk.view.TopTitleView;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import com.wf.domain.UrlDef;

/**
 * Created by fanxs11 on 17/7/4.
 * 晒单——奖励榜
 */

@ContentView(R.layout.ac_rewardlist)
public class RewardListActivity extends BaseActivity {

    @ViewInject(R.id.rewardlist_topview)
    private TopTitleView topView;

    @ViewInject(R.id.powerlist_webview)
    private WebView webView;
    @ViewInject(R.id.powerlist_progress)
    private ProgressBar progressbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    @Override
    public void onRightClick() {
        Intent intent = new Intent(getContext(), RewardHisteryActivity.class);
        startActivityForResult(intent, 0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0 && resultCode == 0 && data != null) {
            String time = data.getStringExtra("time");
            webView.loadUrl(getUrl()+"?time="+time);
        }
    }

    private String getUrl() {
        return UrlDef.followUrl + "shaidan/reward.html?source=" + BuildConfig.UPDATE_KEY;
    }

    private void initView() {
        topView.setOnTitleClickListener(this);
//        topView.setRightTxt("往届奖励");
//        topView.getRightTxt().setGravity(Gravity.RIGHT);
//        topView.getRightTxt().setBackground(null);

        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setBuiltInZoomControls(true);
        WebSettings webSettings = webView.getSettings();
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setSaveFormData(false);
        // 必须要打开javaScript
        webSettings.setJavaScriptEnabled(true);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setSupportZoom(true);
        webView.setWebChromeClient(new MyWebChromeClient());
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if (newProgress == 100) {
                    progressbar.setVisibility(View.GONE);
                } else {
                    if (progressbar.getVisibility() == View.GONE) {
                        progressbar.setVisibility(View.VISIBLE);
                    }
                    progressbar.setProgress(newProgress);
                }
                super.onProgressChanged(view, newProgress);
            }
        });
        webView.loadUrl(getUrl());

    }

    class MyWebChromeClient extends WebChromeClient {
        @Override
        public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
            // message 参数 。。
            Log.d(TAG, "onJsAlert: message"+message);
            toIntent(message);
            result.confirm();
            return true;
        }
    }

    protected void toIntent(String message) {

    }
}
